package controlleur;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import annuaire.beans.Student;
import annuaire.rmi.remotedObject.IRemotedAnnuaire;
import form.bean.SearchStudentForm;

/**
 * Controlleur responsable de la mise a jour d'un etudiant
 * 
 * @author Yannick Lorenzati & Stephanie Malakian
 * 
 */
public class UpdateStudentMainControlleur extends SimpleFormController {

	/** Logger pour cette classe ainsi que pour ses sous classes */
	protected final Log logger = LogFactory.getLog(getClass());

	/**
	 * Reference vers l'objet distant Annuaire
	 */
	private IRemotedAnnuaire remotedAnnuaire;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax
	 * .servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
	 * java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object commandAjout,
			BindException errors) throws Exception {

		SearchStudentForm searchForm = (SearchStudentForm) commandAjout;
		Student pattern = createStudentSearchPattern(searchForm);

		if (pattern != null) {
			request.getSession().setAttribute("updateStudentMain.pattern",
					pattern);
		}

		ModelAndView mav = showForm(request, response, errors);
		return mav;
	}

	/**
	 * Creer un motif de recherche a partir d'un bean de formulaire de recherche
	 * 
	 * @param searchForm
	 * @return
	 */
	private Student createStudentSearchPattern(SearchStudentForm searchForm) {
		Student pattern = null;

		if (searchForm != null
				&& ((searchForm.getFirstName() != null && !searchForm
						.getFirstName().isEmpty())
						|| (searchForm.getName() != null && !searchForm
								.getName().isEmpty())
						|| (searchForm.getLogin() != null && !searchForm
								.getLogin().isEmpty()) || (searchForm
						.getSociety() != null && !searchForm.getSociety()
						.isEmpty()))) {
			pattern = new Student();

			pattern.setLogin(searchForm.getLogin());
			pattern.setFirstName(searchForm.getFirstName());
			pattern.setName(searchForm.getName());
			pattern.setSociety(searchForm.getSociety());
		}

		return pattern;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.springframework.web.servlet.mvc.AbstractFormController#
	 * handleRequestInternal(javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject
	 * (javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws ServletException {

		return new SearchStudentForm();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.SimpleFormController#referenceData
	 * (javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Map<String, Object> referenceData(HttpServletRequest request)
			throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		Collection<Student> listAllStudents = null;

		Student pattern = (Student) request.getSession().getAttribute(
				"updateStudentMain.pattern");
		if (pattern != null) {

			logger.info("*********************************" + pattern);

			listAllStudents = remotedAnnuaire.searchStudent(pattern, 50);
			logger.info("*********************************" + listAllStudents);
			request.getSession().removeAttribute("updateStudentMain.pattern");
		} else {

			Collection<Integer> listIdStudent = remotedAnnuaire
					.findAllStudentsId();
			listAllStudents = new ArrayList<Student>();

			for (Integer id : listIdStudent) {
				listAllStudents.add(remotedAnnuaire.findStudentById(id));
			}
		}
		if (listAllStudents != null) {
			Collections.sort((List<Student>) listAllStudents);
		}

		map.put("listAllStudent", listAllStudents);
		return map;

	}

	/**
	 * @param remotedAnnuaire
	 */
	public void setRemotedAnnuaire(IRemotedAnnuaire remotedAnnuaire) {
		this.remotedAnnuaire = remotedAnnuaire;
	}

	/**
	 * @return
	 */
	public IRemotedAnnuaire getRemotedAnnuaire() {
		return remotedAnnuaire;
	}

}
